﻿using FastCodeGeneration.Core.Framework;
using FastCodeGeneration.Entity;
using FastCodeGeneration.IService;
using FastCodeGeneration.Models;
using FastCodeGeneration.Service;
using Microsoft.AspNetCore.Mvc;

namespace FastCodeGeneration.Controllers
{
    public class ModuleController : Controller
    {
        private readonly IModuleService _moduleService;

        private readonly IProjectService _projectService;

        public ModuleController(IModuleService moduleService, IProjectService projectService)
        {
            _moduleService = moduleService;
            _projectService = projectService;
        }

        public IActionResult Index()
        {
            var projectList = _projectService.SelectAll();
            return View(projectList);
        }

        public IPage<Module> GetCondition(Condition condition)
        {
            var page = _moduleService.SelectByCondition(condition);
            return page;
        }

        public IActionResult AddOrEdit(int id)
        {
            ModuleViewModel viewModel = new ModuleViewModel();
            viewModel.Projects = _projectService.SelectAll();
            viewModel.Entity = _moduleService.SelectById(id) ?? new Module();
            return View(viewModel);
        }
        public AjaxResult Save(Module entity)
        {
            bool res = _moduleService.Save(entity);
            return new AjaxResult { Code = 200, Message = "保存成功", Data = entity };
        }

    }
}
